package com.neuedu.mblog.controller.admin;

import com.neuedu.mblog.controller.BaseController;
import com.neuedu.mblog.entiry.Channel;
import com.neuedu.mblog.entiry.Post;
import com.neuedu.mblog.service.ChannelService;
import com.neuedu.mblog.service.PostService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * 项目：mblog_java4
 * 创建时间：  2021-12-29   9:19
 * 作者 :jshand
 * 描述 : 栏目的后台管理系统
 * /admin/chanel
 *
 * http://localhost/mblog/admin/post
 * http://localhost/mblog/admin/post?_type=list
 */
@WebServlet(urlPatterns = "/admin/post")
public class AdminPostController extends BaseController {
    private PostService postService = new PostService();
    private ChannelService channelService = new ChannelService();



    @Override
    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        List<Post> list = postService.selectList(request);
        request.setAttribute("list",list);
        request.getRequestDispatcher("/WEB-INF/view/admin/post/post_list.jsp").forward(request,response);

    }


    @Override
    protected void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        List<Channel> channelList = channelService.selectList(request);
        request.setAttribute("channelList",channelList);
        request.getRequestDispatcher("/WEB-INF/view/admin/post/post_add.jsp").forward(request,response);
    }

    @Override
    protected void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        boolean success = postService.save(request);

        if(success){
            response.sendRedirect(request.getContextPath()+"/admin/post");
        }else{
            request.getRequestDispatcher("/error.jsp").forward(request,response);
        }
    }



    @Override
    protected void ajaxDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        boolean success = postService.deleteById(request);

        //返回json数据
       super.jsonResult(response,success);
    }



    @Override
    protected void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {


        Post post = postService.selectById(request);
        List<Channel> channelList = channelService.selectList(request);
        request.setAttribute("post",post);
        request.setAttribute("channelList",channelList);
        request.getRequestDispatcher("/WEB-INF/view/admin/post/post_edit.jsp").forward(request,response);

    }

    @Override
    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {

       boolean success = postService.update(request);

       if(success){
            response.sendRedirect(request.getContextPath()+"/admin/post");
       }else{
           request.getRequestDispatcher("/error.jsp").forward(request,response);
       }

    }
}
